package group.igfd.manager.biz.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import group.igfd.manager.biz.entity.OrderItemEntity;
import group.igfd.manager.biz.entity.TransportLogisticsEntity;
import group.igfd.manager.biz.model.OrderItemModel;
import group.igfd.manager.biz.model.QueryOrderModel;
import group.igfd.manager.biz.service.OrderItemService;
import group.igfd.manager.biz.service.OrderRecordService;
import group.igfd.manager.biz.service.TransportLogisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import group.igfd.manager.biz.entity.OrdersEntity;
import group.igfd.manager.biz.service.OrdersService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 订单表
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-10-18 13:24:09
 */
@RestController
@RequestMapping("biz/orders")
@Api(tags = {"A001/ 订单管理"})
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderRecordService orderRecordService;

    @Autowired
    private TransportLogisticsService transportLogisticsService;

    @Autowired
    private OrderItemService orderItemService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("biz:orders:list")
    @ApiOperation(value = "列表")
    public R list(QueryOrderModel params){
        PageUtils page = ordersService.findList(params);

        return R.ok().put("page", page);
    }

    /**
     * 订单详情列表
     * @param orderNo
     * @return
     */
    @GetMapping("/orderItem")
    @RequiresPermissions("biz:orders:list")
    @ApiOperation(value = "订单详情列表")
    public R itemList(@RequestParam String orderNo) {

        List<OrderItemModel> orderItems = ordersService.findOrderItems(orderNo);

        return R.ok().put("items", orderItems);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:orders:info")
    public R info(@PathVariable("id") Integer id){
			OrdersEntity orders = ordersService.selectById(id);

        return R.ok().put("orders", orders);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:orders:save")
    public R save(@RequestBody OrdersEntity orders){
			ordersService.insert(orders);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:orders:update")
    public R update(@RequestBody OrdersEntity orders){
			ordersService.updateById(orders);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:orders:delete")
    public R delete(@RequestBody Integer[] ids){
			ordersService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/addOrderRecord")
    @RequiresPermissions("biz:orders:list")
    @ApiOperation(value = "添加商品记录信息",notes = "type:1,上传小票 2,上传打包信息")
    public R addOrderRecord(@RequestParam Integer type,@RequestParam String[] images,
                            @RequestParam Integer orderItemId,@RequestParam String docketNumber) {

        if (type != 1 && type != 2) {
            return R.error();
        }

        try {
            orderRecordService.add(type,images,orderItemId);
            if (type == 1) {
                OrderItemEntity orderItemEntityUpdate = new OrderItemEntity();
                orderItemEntityUpdate.setId(Integer.valueOf(orderItemId));
                orderItemEntityUpdate.setDocketNumber(docketNumber);
                orderItemService.updateById(orderItemEntityUpdate);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

        return R.ok();
    }

    @PostMapping("/addLogisticsWaybillNumber")
    @RequiresPermissions("biz:orders:list")
    @ApiOperation(value = "上传物流单号")
    public R addLogisticsWaybillNumber(@RequestParam Integer orderItemId,@RequestParam Integer freightId,
                                       @RequestParam String waybillNumber) {

        try {
            transportLogisticsService.add(orderItemId,freightId,waybillNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

        return R.ok();
    }
}
